<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>欢迎注册</title>
    <link rel="stylesheet" href="../layui/layui/css/layui.css">
</head>
<body>
<script src="../layui/layui/layui.js"></script>
<div id="regist" >
    <h2 align="center" id="h2">欢迎注册</h2>
    <form id="registForm" class="layui-form" action="/user/regist" method="post" enctype="multipart/form-data">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="real_name" lay-verify="title" autocomplete="off" placeholder="请输入您的用户名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" lay-verify="pass" placeholder="请输入您的密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="text" name="email" lay-verify="title" autocomplete="off" placeholder="请输入您的邮箱" class="layui-input">
            </div>
        </div>
        <!--        上传图片-->
        <div id="img" >
            <p>请上传您的头像</p>
            <input type="file" id="source" name="source"  required>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<!-- 你的 HTML 代码 -->


<script>
    layui.use(function(){
        var layer = layui.layer,
            form = layui.form,
            $=layui.$;

        form.on('submit(demo)', function(data){
            var from = new FormData(document.getElementById("registForm"));//使用ajax上传文件就需要这个
            console.log(from);
            //debugger //这个代码代表可以在前端的这行代码停住
            $.ajax({
              url:"/user/regist",
              type:"post",
              data:from,//代表获取表单中的数据
              processData:false,  //
              contentType:false,
              success:function (result) {
                  console.log(result);
                  debugger
                   if (result.status==1001){
                       debugger
                       console.log(1)
                       window.location.href="/User/login.html";
                   }else {
                       console.log(2)
                       layer.alert(result.message,{
                        title:'提示信息'
                       });
                   }
              }
          });
            return false;
        });
    });
</script>

<style>
    #img{
        margin-left: 60px;
        margin-bottom: 10px;
    }
    #h2{
        padding-bottom: 20px;
    }

    #regist {
        margin: 100px auto;
        /*这个div距离父块的距离 距离上面100px，其他的居中*/
        /*border: 1px solid red;*/
        width: 600px;
        height: 400px;
        padding: 20px 20px 20px 20px;
        /*    这个div距离第一行内容的距离（向右下左）*/
        box-shadow: 15px 15px 15px 15px #888888;
    }
</style>
</body>
</html>
          